/***************************************************************************
 *   KShootout - online shootout card game.                                *
 *   Copyright (C) 2008, 2009  MacJariel                                   *
 *                                                                         *
 *   KShootout is free software: you can redistribute it and/or modify     *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation, either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   KShootout is distributed in the hope that it will be useful,          *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with KShootout.  If not, see <http://www.gnu.org/licenses/>.    *
 ***************************************************************************/

#include "cardwidget.h"
#include "cardwidgetsizemgr.h"
#include "gamestructs.h"
//#include "gameactionmgr.h"
#include <QPainter>

using namespace client;

CardWidget::CardWidget(QWidget* parent):
        QLabel(parent),
        mp_cardData(new CardData()),
        m_pocketType(POCKET_INVALID),
        m_ownerId(0),
        m_cardSizeRole(CSR_Normal),
        m_hasHighlight(0),
        mp_gameActionMgr(0)
{
    setScaledContents(1);
    connect(CardWidgetSizeMgr::instance(), SIGNAL(cardSizeChanged()),
            this, SLOT(updateSize()));
}

CardWidget::CardWidget(QWidget* parent, GameActionMgr* gameActionMgr, CardType cardType):
        QLabel(parent),
        mp_cardData(new CardData()),
        m_pocketType(POCKET_INVALID),
        m_ownerId(0),
        m_cardSizeRole(CSR_Normal),
        m_hasHighlight(0),
        mp_gameActionMgr(gameActionMgr)
{
    setScaledContents(1);
    connect(CardWidgetSizeMgr::instance(), SIGNAL(cardSizeChanged()),
            this, SLOT(updateSize()));
    mp_cardData->type = cardType;
}

CardWidget::~CardWidget()
{
    delete mp_cardData;
}

void CardWidget::clear()
{
    *mp_cardData = CardData();
}

void CardWidget::setGameActionMgr(GameActionMgr* gameActionMgr)
{
    mp_gameActionMgr = gameActionMgr;
}

void CardWidget::setCardData(const CardData& cardData)
{
    *mp_cardData = cardData;
}

void CardWidget::setPocketType(const PocketType& pocketType)
{
    m_pocketType = pocketType;
}

void CardWidget::setOwnerId(PlayerId ownerId)
{
    m_ownerId = ownerId;
}

void CardWidget::setCardSizeRole(CardSizeRole cardSizeRole)
{
    m_cardSizeRole = cardSizeRole;
}

void CardWidget::setHighlight(bool hasHighlight)
{
    m_hasHighlight = hasHighlight;
    update();
}

void CardWidget::unrevealCard()
{
    mp_cardData->name = "";
}

void CardWidget::setPlayerRoleCard(PlayerRole playerRole)
{
    mp_cardData->name = playerRoleToString(playerRole);
    if (playerRole == ROLE_UNKNOWN) {
        mp_cardData->name = "";
    }
    mp_cardData->type = CARDTYPE_ROLE;
}

void CardWidget::setCharacterCard(const QString& character)
{
    mp_cardData->name = character;
    mp_cardData->type = CARDTYPE_CHARACTER;
}

void CardWidget::updatePixmap()
{
///@todo Uncomment as soon as CardBank and Card is ready
#if 0

    QPixmap pixmap;
    if (!mp_cardData->name.isEmpty() || mp_cardData->type != CARDTYPE_UNKNOWN) {
        const Card* card = CardBank::instance().card(*mp_cardData);
        if (card != 0) {
            pixmap = card->pixmap(*mp_cardData);
        }
    }
    setPixmap(pixmap);
#endif
    updateSize();
}

void CardWidget::updateSize()
{
    const QSize& size = CardWidgetSizeMgr::instance()->cardSize(m_cardSizeRole);
    setMinimumSize(size);
    setMaximumSize(size);
    resize(size);
    updateGeometry();
}

/* virtual */
void CardWidget::mousePressEvent(QMouseEvent* e)
{
    if (mp_gameActionMgr == 0) {
        qWarning("CardWidget without set gameActionManager clicked.");
        return;
    }
///@todo Implement as soon as GameActionMgr is ready
#if 0
    switch(e->button()) {
        case Qt::LeftButton: {
            bool handled = mp_game->actionManager()->onCardClicked(this);
            if (!handled)
                QLabel::mousePressEvent(e);
            break;
        }
        case Qt::RightButton:
            mp_game->actionManager()->onCardRightClicked(this);
            break;
        default:
            break;
    }
#endif
}

/* virtual */
void CardWidget::paintEvent(QPaintEvent *event)
{
    QLabel::paintEvent(event);
    if (m_hasHighlight) {
        QPainter painter(this);
        painter.fillRect(this->rect(), QBrush(QColor(0,0,0,128)));
    }
}
